package com.study.springmvc.v9mvc.interceptor;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {

    //请求方法前置拦截, 返回true 表示会执行到目标方法, 返回false 表示不执行目标方法
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println(">>>>>>>>>>preHandle");
        String token = request.getParameter("token");
        if (StringUtils.isEmpty(token)) {
            response.setStatus(500);
            response.getWriter().write("toek is null");
            return false;
        }
        return true;

    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        System.out.println(">>>>>>>>>>>>>postHandle");
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println(">>>>>>>>>>>>afterCompletion");
    }
}
